package com.key.w8.http1.entity;

import com.key.w8.http1.factory.FormData;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpVersion;

import javax.swing.plaf.PanelUI;
import java.lang.ref.PhantomReference;
import java.nio.ByteBuffer;
import java.util.List;

/**
 * @Author: k8
 * @CreateTime: 2025-03-10
 * @Version: 1.0
 */

/**
 * 在获取到请求或者响应的时候inbound，可以将content转化为复杂对象，根据解析器
 * 但是在写请求或者响应的时候，不能以复杂对象的形式传递，必须解析成基本的三个类型
 * 否则{@link com.key.w8.http1.handler.codec.Http1Codec#writeRequest(ChannelPromise, HttpRequest)}
 * {@link com.key.w8.http1.handler.codec.Http1Codec#writeResponse(ChannelPromise, HttpResponse)}
 * 将不处理
 */
public class HttpObject {
    private byte[] data;
    private ByteBuffer buffer;
    private ByteBuf content;
    private String message;
    //由解析器解析的复杂对象
    private Object complexObject;
    private FormData formData;
    private HttpHeaders headers;
    private HttpVersion version;
    private DataContentType contentType = DataContentType.NULL;

    public HttpObject(HttpHeaders headers, HttpVersion version) {
        assert headers != null;
        assert version != null;
        this.headers = headers;
        this.version = version;
    }

    public ByteBuf content() {
        return content;
    }

    public void setContent(ByteBuf content) {
        assert content != null;
        this.content = content;
        this.contentType = DataContentType.BUF;
    }

    public ByteBuffer buffer() {
        return buffer;
    }
    public DataContentType contentType(){
        return this.contentType;
    }
    public void setBuffer(ByteBuffer buffer) {
        assert buffer != null;
        this.buffer = buffer;
        this.contentType = DataContentType.BUFFER;
    }

    public HttpHeaders headers() {
        return headers;
    }

    public void setHeaders(HttpHeaders headers) {
        this.headers = headers;
    }

    public HttpVersion version() {
        return version;
    }

    public void setVersion(HttpVersion version) {
        this.version = version;
    }
    public void setData(byte[] data){
        assert data != null;
        this.data = data;
        this.contentType = DataContentType.DATA;
    }
    public byte[] data(){
        return this.data;
    }

    public void setMessage(String message) {
        assert message != null;
        this.message = message;
        this.contentType = DataContentType.MESSAGE;
    }

    public String message() {
        return message;
    }
    public void setComplexObject(Object o){
        assert o != null;
        this.complexObject = o;
        this.contentType = DataContentType.COMPLEX_OBJECT_FROM_READ;
    }
    public Object complexObject(){
        return this.complexObject;
    }

    public void setFormData(FormData formData){
        assert formData != null;
        this.formData = formData;
        this.contentType = DataContentType.FORM_DATA;
    }
    public FormData formData(){
        return formData;
    }

    /**
     * 不同于Http请求头字段ContentType
     */
    public enum DataContentType {
        COMPLEX_OBJECT_FROM_READ,
        BUFFER,
        DATA,
        MESSAGE,
        BUF,
        FORM_DATA,
        NULL
    }
}
